<template>
    <panelHead :route="route" />
    <div class="form">
        <el-form :inline="true" :model="searchForm" label-width="80px">
            <el-form-item label="订单号" prop="out_trade_no">
                <el-input v-model="searchForm.out_trade_no" placeholder="请输入订单号" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">查询</el-button>
            </el-form-item>
        </el-form>
    </div>
    <el-table :data="tableData.list" stripe style="width: 100%">
        <el-table-column prop="out_trade_no" label="订单号" width="150" />
        <el-table-column prop="hospital_name" label="就诊医院" width="100" />
        <el-table-column prop="service_name" label="陪诊服务" width="150" />
        <el-table-column label="陪诊师" width="70">
            <template #default="scope">
                <el-image style="width: 40px;" :src="scope.row.serviceImg" alt="" />
            </template>
        </el-table-column>
        <el-table-column prop="tel" label="陪护师手机号" width="200" />
        <el-table-column prop="price" label="总价" width="100" />
        <el-table-column prop="paidPrice" label="已支付" width="100" />
        <el-table-column label="下单时间" width="200">
            <template #default="scope">
                {{ dayjs(scope.row.order_start_time).format('YYYY-MM-DD HH:mm:ss') }}
            </template>
        </el-table-column>
        <el-table-column label="订单状态" width="150">
            <template #default="scope">
                <el-tag :type="statusMap(scope.row.status)">{{ scope.row.trade_state }}</el-tag>
            </template>
        </el-table-column>
        <el-table-column prop="service_state" label="接单状态" width="130" />
        <el-table-column prop="tel" label="联系人手机号" width="150" />
        <el-table-column label="操作" width="130" fixed="right">
            <template #default="scope">
                <el-popconfirm v-if="scope.row.trade_state === '待服务'" title="是否确定完成?" @confirm="confirmEvent(scope.row.out_trade_no)">
                    <template #reference>
                        <el-link type="primary">服务完成</el-link>
                    </template>
                </el-popconfirm>
                <el-link v-else disabled type="primary">暂无服务</el-link>
            </template>
        </el-table-column>
    </el-table>
    <el-pagination v-model:current-page="paginationData.pageNum" :page-size="paginationData.pageSize" :size="size"
        :page-sizes="[5, 10, 20, 30]" layout="total, sizes, prev, pager, next, jumper" :total="tableData.total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
</template>

<script setup>
import { onMounted, reactive } from 'vue';
import { useRoute } from 'vue-router';
import { adminOrder,updateOrder } from '../../../api/index';
import dayjs from 'dayjs';

//表单搜索
const searchForm = reactive({
    out_trade_no: ''
});

const onSubmit = () => {
    paginationData.out_trade_no = searchForm.out_trade_no;
    getListData();
}

const route = useRoute();//当前路由

onMounted(() => {
    getListData();
})

const paginationData = reactive({//分页数据
    pageNum: "1",
    pageSize: "10",
    out_trade_no: ''
});

const tableData = reactive({//表格数据
    list: [],
    total: 0
});

const getListData = () => {
    adminOrder(paginationData).then(({ data }) => {
        const { list, total } = data.data;
        tableData.list = list;
        tableData.total = total;
    })
}

const statusMap = (key) => {//状态映射
    const obj = {
        '已取消': 'info',
        '待支付': 'warning',
        '已完成': 'success'
    }
    return obj[key] || '--';
};

const handleSizeChange = (val) => {
    paginationData.pageSize = val;
    getListData();
}

const handleCurrentChange = (val) => {
    paginationData.pageNum = val;
    getListData();
}

const confirmEvent=(out_trade_no)=>{
    updateOrder({id:out_trade_no}).then(()=>{
        getListData();
    })
}
</script>

<style lang="less" scoped>
.form {
    display: flex;
    justify-content: flex-end;
    padding: 10px 0 10px 10px;

    .el-form {
        margin: 0;
    }

}
</style>